package com.webside.file.controller;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.webside.file.result.FileResult;
import com.webside.listener.BaseData;

@RestController
@RequestMapping("videoFiles")
public class VideoFileController {
	private static final Logger log = Logger.getLogger(FileController.class);
	
	@RequestMapping(method = RequestMethod.POST, value = "/upload")
	@ResponseBody
	public Object upload(@RequestParam MultipartFile video) {
		int error=0;
		String returnFileName=null;
		try {
			String origName=video.getOriginalFilename();
			String currentName=System.currentTimeMillis()+"-"+origName;
			returnFileName=BaseData.origin_path+currentName;
			File file=new File(BaseData.video_folder+returnFileName);
			file.createNewFile();
			video.transferTo(file);
		} catch (IllegalStateException e) {
			error++;
			e.printStackTrace();
		} catch (IOException e) {
			error++;
			e.printStackTrace();
		}
		
		if(error==0){
			return new FileResult(returnFileName,"0",1);
		}else{
			return new FileResult(returnFileName,"0",0);
		}
	}

}
